package stream.training;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZH
 */
public class TrainingDemo {
    public static void main(String[] args) {
        //构造商品
        Goods bread = new Goods("面包", 10.0);
        Goods milk = new Goods("牛奶", 12.0);
        Goods juice = new Goods("果汁", 6.0);
        Goods ham = new Goods("火腿", 20.0);
        //构造订单
        List<Order> orders = Arrays.asList(
                new Order(ham, 2011, 300),
                new Order(bread, 2012, 1000),
                new Order(bread, 2011, 400),
                new Order(milk, 2012, 710),
                new Order(milk, 2012, 700),
                new Order(juice, 2012, 950)
        );
        //test1(orders);
        //test2(orders);
        //test3(orders);
        //test4(orders);
        test5(orders);
    }

    /**
     * 找出2011年所有的订单，按商品数量从高到低排序
     */
    private static void test1(List<Order> orders){
        orders.stream()
                .filter(test1 -> test1.getYear() == 2011)
                .sorted(Comparator.comparing(Order::getTotal).reversed())
                .forEach(System.out::println);
    }

    /**
     * 获取所有订单里的所有商品的单价
     * @param orders
     */
    private static void test2(List<Order> orders){
        orders.stream()
                .map(order -> order.getGoods().getPrice())
                .collect(Collectors.toSet())
                .forEach(System.out::println);
    }
    /**
     * 查找订单中所有单价为12的商品
     * @param orders
     */
    private static void test3(List<Order> orders){
        orders.stream()
                .filter(order -> order.getGoods().getPrice() == 12)
                .map(Order::getGoods)
                .collect(Collectors.toSet())
                .forEach(System.out::println);
    }

    /**
     * 查所有商品的名字,拼接成一个符串
     * @param orders
     */
    private static void test4(List<Order> orders){
        String collect = orders.stream()
                .map(order -> order.getGoods().getName())
                .distinct()
                .collect(Collectors.joining(","));
        System.out.println(collect);
    }

    /**
     * 判断所有订单中是否有价格为20的商品
     * @param orders
     */
    private static void test5(List<Order> orders){
        boolean b = orders.stream()
                .anyMatch(order -> order.getGoods().getPrice() == 20);
        System.out.println(b);
    }

}
